Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13-wnsmir #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

13-wnsmir #54

wants to merge 1 commit into from

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Jan 7, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค ์‹ค์ „๋ฌธ์ œ์ค‘ chapter ์ตœ๋‹จ๊ฒฝ๋กœ์˜ 1๋ฒˆ๋ฌธ์ œ ๋ฐœ์ทŒ์ž…๋‹ˆ๋‹ค.
๊ท ํ˜ธ๋‹˜ 12๋ฒˆ์งธ PR๊ณผ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ๋ณต์Šต๊ฒธ ํ’€์–ด๋ณด๊ณ ์ž ๊ฐ€์ ธ์™€๋ดค์Šต๋‹ˆ๋‹ค.
๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅธ์ ‘๊ทผ์œผ๋กœ ํ’€์–ด ์ข€ ์˜ค๋ž˜๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค,,

๋ฏธ๋ž˜๋„์‹œ - ๋‚œ์ด๋„2/3 - ํ’€์ด์‹œ๊ฐ„ 40min - ๋ฉ”๋ชจ๋ฆฌ์ œํ•œ 128MB
๋ฌธ์ œ
๋ฐฉ๋ฌธํŒ๋งค์› A๋Š” ํ˜„์žฌ 1๋ฒˆํšŒ์‚ฌ์— ์œ„์น˜ํ•ด์žˆ์œผ๋ฉฐ X๋ฒˆ ํšŒ์‚ฌ์— ๋ฐฉ๋ฌธํ•ด ๋ฌผ๊ฑด์„ ํŒ๋งคํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋ฏธ๋ž˜๋„์‹œ์—์„œ ํšŒ์‚ฌ๋Š” ํšŒ์‚ฌ๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ํ†ต๋กœ๋กœ๋งŒ ์ด๋™๊ฐ€๋Šฅํ•˜๊ณ , ์ด ํ†ต๋กœ๋Š” ํ•ญ์ƒ ์–ด๋””๋“  1์˜์‹œ๊ฐ„์œผ๋กœ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
A๋Š” 1๋ฒˆ์—์„œ X๋ฒˆ๊นŒ์ง€ ๊ฐ€์•ผํ•˜๋Š”๋ฐ ๋„์ค‘ ์†Œ๊ฐœํŒ…์„ ํ•ด์•ผํ•œ๋‹ค. ์†Œ๊ฐœํŒ… ์ƒ๋Œ€๋Š” K๋ฒˆ ํšŒ์‚ฌ๋ฅผ ๋ฐฉ๋ฌธํ•œ ๋’ค XํšŒ์‚ฌ๋กœ ๊ฐ€๋Š”๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค.

ํ˜„์œ„์น˜์—์„œ K๋ฅผ๊ฑฐ์ณ X๋กœ ์ด๋™ํ•˜๋Š” A์˜ ์ตœ๋‹จ ์ด๋™์‹œ๊ฐ„์„ ๊ตฌํ•˜์—ฌ๋ผ.

์ž…๋ ฅ์€ ์ฒซ์ค„์— N๊ณผM์ด ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง€๊ณ , ๋‹ค์Œ M๊ฐœ์˜ ์ค„์— ๊ฐ ํšŒ์‚ฌ๋“ค์˜ ํ†ต๋กœ ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งˆ์ง€๋ง‰์ค„์€ X์™€ K๊ฐ€ ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
(1<=N, M<=100), (1<=K<=100)
์ž…๋ ฅ์˜ˆ์‹œ
5 7
1 2
1 3
1 4
2 4
3 4
3 5
4 5
4 5
์ถœ๋ ฅ์˜ˆ์‹œ
3

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด์ „ ๊ท ํ˜ธ๋‹˜ ๋ฌธ์ œ๋Š” BFS๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ์š”, ์ด ๋ฌธ์ œ๋Š” ๋ชจ๋“  ํ†ต๋กœ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 1์ด๋ผ BFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์ง€๋งŒ, ๋‹ค๋ฅด๊ฒŒ ํ’€์–ด๋ณด๊ธฐ ์œ„ํ•ด ๊ฑฐ์ณ๊ฐ€๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ”Œ๋กœ์ด๋“œ์œ„์…œ์„ ํ™œ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ํ”Œ๋ฃจ์ด๋“œ ์œ„์…œ์€ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋™์ ๋ฐฐ์—ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ผ์ข…์ธ๋ฐ,
์ธ์ ‘ํ–‰๋ ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์ถœ๋ฐœ๋…ธ๋“œ์™€ ๋„์ฐฉ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ N๋ฒˆ์”ฉ ๋Œ์•„๊ฐ€๋ฉฐ ์ตœ์†Œํ•œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ œ์ผ ๋ฐ”๊นฅ ๋ฐ˜๋ณต๋ฌธ K๋ฅผ ํ™œ์šฉํ•ด ์ถœ๋ฐœ๊ณผ ๋„์ฐฉ๋…ธ๋“œ ์‚ฌ์ด์— ๋‹ค๋ฅธ๋…ธ๋“œ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์—…๋ฐ์ดํŠธํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
์ฆ‰ 1๊ณผ 3์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 3์ธ๋ฐ 1->2->3์ด 2๋ผ๋ฉด 1๊ณผ2, 2์™€3์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์œผ๋กœ 1->3์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฎ์–ด์”Œ์šฐ๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋ฐ˜๋ณต๋ฌธ 3๋ฒˆ N^3์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ์ด์ „ ๋ฆฌ๋ทฐ์—์„œ๋„ ์ผ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ทธ๋Œ€๋กœ ๋‹ค์‹œ ์จ๋ดค์Šต๋‹ˆ๋‹ค.
(๋‹ค์Œ์— ์ด๋Ÿฐ๊ฒŒ ๋‚˜์˜ค๋ฉด ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ๋กœ....)

ํŠน๋ณ„ํ•œ ๊ธฐ์ˆ ์€ ์—†์ง€๋งŒ ์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•œ ํ˜•ํƒœ๋กœ ์กฐ๊ธˆ ๋ณ€ํ˜•ํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ผ๋•Œ

if (cur == e) return distance;

์ด๋ ‡๊ฒŒ ๋…ธ๋“œ๋งˆ๋‹ค ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋ฉด์„œ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋ฉด ๋ฐ”๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ทธ๋Ÿฐ ๋กœ์ง์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ € if๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๊ฐ€ return๋˜๋ฉด ๋ชฉ์ ์ง€๋กœ๊ฐ€๋Š” ๊ธธ์ด ์—†๋‹ค๋Š” ๋œป์ด๊ธฐ์— -1์„ return ํ–ˆ์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N, M;
vector<vector<int>> graph;

int bfs(int s, int e) {
    vector<bool> visited(N + 1, false);

    queue<int> q;
    q.push(s);
    visited[s] = true;

    int distance = 0;
    while (!q.empty()) {
        int size = q.size();
        while (size--) {
            int cur = q.front();
            q.pop();

            if (cur == e) return distance;
            for (int v : graph[cur]) {
                if (!visited[v]) {
                    q.push(v);
                    visited[v] = true;
                }
            }
        }
        distance++;
    }

    return -1;
}

int main() {
    cin >> N >> M;

    graph = vector<vector<int>>(N + 1);
    for (int i = 1; i <= M; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    int X, K;
    cin >> X >> K;

    int temp1 = bfs(1, K);
    int temp2 = bfs(K, X);
    if (temp1 == -1 || temp2 == -1) cout << "-1\n";
    else cout << temp1 + temp2;

    return 0;
}

Comment on lines +20 to +23
for k in range(1, N+1):
for a in range(1, N+1):
for b in range(1, N+1):
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ด๊ฒŒ ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ตฐ์š” ๋‹ค์Œ์—๋Š” ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์จ๋ด์•ผ๊ฒ ๋„ค์š”...

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์„ ์ž˜ ๋ชฐ๋ผ์„œ bfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋งŒ์•ฝ ๊ฐ€์ค‘์น˜๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฐ’์ด ์ฃผ์–ด์ง„๋‹ค๋ฉด ์—„์ฒญ ํ—ค๋งธ์„ ๊ฒƒ ๊ฐ™๋„ค์š”..

๊ธฐ์กด bfs๋กœ์ง์— ์ƒˆ๋กœ์šด ๊ฑธ ์ถ”๊ฐ€ํ•ด์•ผํ•˜๋‚˜ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ตฌํ˜„ํ•˜๋‹ค ๋ณด๋‹ˆ ํ•˜๋‚˜์˜ bfs๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊ฑฐ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์— ๊ฝค๋‚˜ ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ bfs ๊ตฌํ˜„ํ•  ๋•Œ ์ฃผ์˜ํ•  ๊ฒƒ์ด ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜๊ธฐ์— visited์™€ queue๋ฅผ ์ƒˆ๋กœ์šด ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ด์„œ ๊ทธ ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ ธ๋„ค์š”. ์ฐธ๊ณ ๋กœ graph๋Š” ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค

int bfs(int start, int dest) {
    if (start == dest) return 0;

    int distance = 0;
    bool visited[101] = {false};
    queue<int> q;

    visited[start] = true;
    q.push(start);
    while (!q.empty()) {
        int size = q.size();
        while (size--) {
            int cur = q.front();
            q.pop();

            for (int adj: graph[cur]) {
                if (adj == dest) return ++distance;
                if (!visited[adj]) {
                    visited[adj] = true;
                    q.push(adj);
                }
            }
        }
        distance++;
    }
    return -1;
}

์ค€์šฉ๋‹˜๊ณผ ๋‹ค๋ฅธ pr์˜ ๊ทผํ˜ธ๋‹˜ ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. :) ๋‹ค์Œ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์จ๋จน์–ด์•ผ๊ฒ ์–ด์š”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants